package com.wisdytech.ccdserver.matchwarning.serveice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wisdytech.ccdserver.matchwarning.dao.IWarningEquipmentCameraDao;
import com.wisdytech.ccdserver.matchwarning.dao.IWarningEquipmentDao;
import com.wisdytech.ccdserver.matchwarning.model.WarningEquipment;
import com.wisdytech.ccdserver.matchwarning.model.WarningEquipmentCamera;
import com.wisdytech.ccdserver.matchwarning.serveice.IWarningEquipmentService;
import com.wisdytech.ccdserver.matchwarning.vo.WarningEquipmentVo;
import com.wisdytech.common.bean.BaseModel;
import com.wisdytech.common.bean.BasePage;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.utils.BaseUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author sunjinxin
 * @date 2024/5/20
 */
public class WarningEquipmentServiceImpl extends ServiceImpl<IWarningEquipmentDao, WarningEquipment> implements IWarningEquipmentService {

    @Resource
    private IWarningEquipmentCameraDao warningEquipmentCameraDao;

    @Override
    public IPage<WarningEquipment> getWarningEquipmentList(Filter filter) {
        Page<WarningEquipment> page = BasePage.getPage(filter);
        IPage<WarningEquipment> warningEquipmentIPage = baseMapper.getWarningEquipmentList(page, filter.getQueryMap());
        return warningEquipmentIPage;
    }

    @Override
    public Boolean saveWarningEquipment(WarningEquipmentVo warningEquipmentVo, String username) {
        LambdaQueryWrapper<WarningEquipment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarningEquipment::getEquipmentCode, warningEquipmentVo.getEquipmentCode());
        queryWrapper.eq(BaseModel::getDelFlag, "0");
        WarningEquipment warningEquipmentQuery = baseMapper.selectOne(queryWrapper);

        if (warningEquipmentQuery == null ) {
            WarningEquipment warningEquipment = new WarningEquipment();
            BeanUtils.copyProperties(warningEquipmentVo,warningEquipment);
            BaseUtils.setDefaultFiledsBaseModel(warningEquipment, username, true);
            baseMapper.insert(warningEquipment);
            if(warningEquipmentVo.getCameraCode() != null && warningEquipmentVo.getUpNum() != null){
                saveCamera(warningEquipmentVo,warningEquipment.getId(),username);
            }
        } else {
            BaseUtils.setDefaultFiledsBaseModel(warningEquipmentQuery, username, false);
            warningEquipmentQuery.setMatchNum(warningEquipmentVo.getMatchNum());
            if(warningEquipmentVo.getCameraCode() != null && warningEquipmentVo.getUpNum() != null){
                saveCamera(warningEquipmentVo,warningEquipmentQuery.getId(),username);
            }
            baseMapper.updateById(warningEquipmentQuery);
        }
        return true;
    }

    private void saveCamera(WarningEquipmentVo warningEquipmentVo,String equipmentId,String username) {
        LambdaQueryWrapper<WarningEquipmentCamera> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarningEquipmentCamera::getEquipmentId, equipmentId);
        queryWrapper.eq(WarningEquipmentCamera::getCameraCode,warningEquipmentVo.getCameraCode());
        WarningEquipmentCamera warningEquipmentCameraQuery = warningEquipmentCameraDao.selectOne(queryWrapper);
        if(warningEquipmentCameraQuery == null){
            WarningEquipmentCamera warningEquipmentCamera = new WarningEquipmentCamera();
            warningEquipmentCamera.setCameraCode(warningEquipmentVo.getCameraCode());
            warningEquipmentCamera.setUpNum(warningEquipmentVo.getUpNum());
            warningEquipmentCamera.setEquipmentId(equipmentId);
            BaseUtils.setDefaultFiledsBaseModel(warningEquipmentCamera, username, true);
            warningEquipmentCameraDao.insert(warningEquipmentCamera);
        }else{
            warningEquipmentCameraQuery.setUpNum(warningEquipmentVo.getUpNum());
            BaseUtils.setDefaultFiledsBaseModel(warningEquipmentCameraQuery, username, false);
            warningEquipmentCameraDao.updateById(warningEquipmentCameraQuery);
        }
    }
}
